package diploma.kisel.modelas.gui.common;

import static diploma.kisel.modelas.service.StaticHelper.nullToEmptyString;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import diploma.kisel.modelas.domain.Application;
import diploma.kisel.modelas.domain.Computer;
import diploma.kisel.modelas.domain.Database;
import diploma.kisel.modelas.domain.Device;
import diploma.kisel.modelas.domain.Program;
import diploma.kisel.modelas.domain.Workplace;
import diploma.kisel.modelas.domain.Workstation;
import diploma.kisel.modelas.domain.WorkstationType;

/**
 * Contains default models for all tables.
 * 
 * @author Vgerton2
 * 
 */
public class TablesSettings {

	private static Vector<Vector<Object>> tableDataPC;
	private static Vector<Vector<Object>> tableDataDevice;
	private static Vector<Vector<Object>> tableDataForProgram;
	private static Vector<Vector<Object>> tableDataForApplication;
	private static Vector<Vector<Object>> tableDataForDatabase;
	private static Vector<Vector<Object>> tableDataForServer;
	private static Vector<Vector<Object>> tableDataForWorkplace;

	public static DefaultTableModel modelSettingForDevice(
			DefaultTableModel model, Set<Device> devices) {

		tableDataDevice = new Vector<Vector<Object>>();
		// List<Computer> computers=computerService.findAll();
		for (Device device : devices) {
			Vector<Object> currRow = new Vector<Object>();
			currRow.add(device.getId());
			currRow.add(nullToEmptyString(device.getNumber()));
			currRow.add(nullToEmptyString(device.getBrand()));
			currRow.add(device.getCost());
			currRow.add(nullToEmptyString(device.getFeatures()));
			currRow.add(device.getDeviceType().getId());
			tableDataDevice.add(currRow);
		}

		Vector<String> columnName = new Vector<String>();
		columnName.add("Ид");
		columnName.add("Номер");
		columnName.add("Марка");
		columnName.add("Стоимость");
		columnName.add("Характеристики");
		columnName.add("Тип");

		model = new DefaultTableModel(tableDataDevice, columnName) {

			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int col) {
				return false;
			}

		};

		return model;
	}

	public static DefaultTableModel modelSettingForPC(DefaultTableModel model,
			Set<Computer> computers) {

		tableDataPC = new Vector<Vector<Object>>();
		// List<Computer> computers=computerService.findAll();
		for (Computer computer : computers) {
			Vector<Object> currRow = new Vector<Object>();
			currRow.add(computer.getId());
			currRow.add(nullToEmptyString(computer.getNumber()));
			currRow.add(nullToEmptyString(computer.getBrand()));
			currRow.add(computer.getCost());
			currRow.add(nullToEmptyString(computer.getFeatures()));
			currRow.add(computer.getType());
			tableDataPC.add(currRow);
		}

		Vector<String> columnName = new Vector<String>();
		columnName.add("Ид");
		columnName.add("Номер");
		columnName.add("Марка");
		columnName.add("Стоимость");
		columnName.add("Характеристики");
		columnName.add("Тип");

		model = new DefaultTableModel(tableDataPC, columnName) {

			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int col) {
				return false;
			}

		};

		return model;
	}

	public static DefaultTableModel modelSettingForProgram(
			DefaultTableModel model, Set<Program> programs) {

		tableDataForProgram = new Vector<Vector<Object>>();
		// List<Computer> computers=computerService.findAll();
		for (Program program : programs) {
			Vector<Object> currRow = new Vector<Object>();
			currRow.add(program.getId());
			currRow.add(nullToEmptyString(program.getNumber()));
			currRow.add(nullToEmptyString(program.getName()));
			currRow.add(nullToEmptyString(program.getVersion()));
			currRow.add(program.getCost());
			currRow.add(nullToEmptyString(program.getRequirements()));
			currRow.add(program.getProgramType());
			tableDataForProgram.add(currRow);
		}

		Vector<String> columnName = new Vector<String>();
		columnName.add("Ид");
		columnName.add("Номер");
		columnName.add("Название");
		columnName.add("Версия");
		columnName.add("Стоимость");
		columnName.add("Требования");
		columnName.add("Тип");

		model = new DefaultTableModel(tableDataForProgram, columnName) {

			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int col) {
				return false;
			}

		};

		return model;
	}

	public static DefaultTableModel modelSettingForApplication(
			DefaultTableModel model, Set<Application> applications) {

		tableDataForApplication = new Vector<Vector<Object>>();
		// List<Computer> computers=computerService.findAll();
		for (Application application : applications) {
			Vector<Object> currRow = new Vector<Object>();
			currRow.add(application.getId());
			currRow.add(nullToEmptyString(application.getNumber()));
			currRow.add(nullToEmptyString(application.getName()));
			currRow.add(nullToEmptyString(application.getShortName()));
			currRow.add(application.getCost());
			tableDataForApplication.add(currRow);
		}

		Vector<String> columnName = new Vector<String>();
		columnName.add("Ид");
		columnName.add("Номер");
		columnName.add("Название");
		columnName.add("Cокр. Назв.");
		columnName.add("Стоимость");

		model = new DefaultTableModel(tableDataForApplication, columnName) {

			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int col) {
				return false;
			}

		};

		return model;
	}

	public static DefaultTableModel modelSettingForDatabase(
			DefaultTableModel model, Set<Database> databases) {

		tableDataForDatabase = new Vector<Vector<Object>>();
		// List<Computer> computers=computerService.findAll();
		for (Database database : databases) {
			Vector<Object> currRow = new Vector<Object>();
			currRow.add(database.getId());
			currRow.add(nullToEmptyString(database.getNumber()));
			currRow.add(database.getCountOfTables());
			currRow.add(database.getCountOfCreationDays());
			currRow.add(database.getDataCapacity());
			currRow.add(database.getCost());
			tableDataForDatabase.add(currRow);
		}

		Vector<String> columnName = new Vector<String>();
		columnName.add("Ид");
		columnName.add("Номер");
		columnName.add("Кол-во таблиц");
		columnName.add("Кол-во дней");
		columnName.add("Объем данных(Кб)");
		columnName.add("Стоимость");

		model = new DefaultTableModel(tableDataForDatabase, columnName) {

			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int col) {
				return false;
			}

		};

		return model;
	}

	public static DefaultTableModel modelSettingForServers(
			DefaultTableModel model, List<Workstation> workstations) {

		tableDataForServer = new Vector<Vector<Object>>();
		// List<Computer> computers=computerService.findAll();
		for (Workstation workstation : workstations) {
			if (workstation.getWorkstationType().equals(WorkstationType.Server)) {
				Vector<Object> currRow = new Vector<Object>();
				currRow.add(workstation.getId());
				currRow.add(nullToEmptyString(workstation.getNumber()));
				currRow.add(workstation.getComputers().size());
				currRow.add(workstation.getDevices().size());
				currRow.add(workstation.getCommonDatabases().size());
				int totalPrice = 0;
				Set<Computer> computers = workstation.getComputers();
				for (Computer computer : computers) {
					totalPrice += computer.getCost();
				}
				Set<Device> devices = workstation.getDevices();
				for (Device device : devices) {
					totalPrice += device.getCost();
				}
				Set<Database> databases = workstation.getCommonDatabases();
				for (Database database : databases) {
					if (database.getCost() != null)
						totalPrice += database.getCost();
				}
				currRow.add(totalPrice);
				tableDataForServer.add(currRow);
			}
		}

		Vector<String> columnName = new Vector<String>();
		columnName.add("Ид");
		columnName.add("Номер");
		columnName.add("Кол-во ПЭВМ");
		columnName.add("Кол-во Устройств");
		columnName.add("Кол-во БД");
		columnName.add("Стоимость");

		model = new DefaultTableModel(tableDataForServer, columnName) {

			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int col) {
				return false;
			}

		};

		return model;
	}	

	public static void makeFirstColumnInvisible(JTable table) {
		TableColumn col = table.getColumnModel().getColumn(0);
		col.setMinWidth(0);
		col.setMaxWidth(0);
	}

}
